package com.kefan.reptilemanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kefan.reptilemanagement.common.entities.Illegal;
import com.kefan.reptilemanagement.common.errorHandler.BizException;
import com.kefan.reptilemanagement.mapper.IllegalMapper;
import com.kefan.reptilemanagement.service.IllegalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjj
 * @since 2024-05-03
 */
@Service
public class IllegalServiceImpl extends ServiceImpl<IllegalMapper, Illegal> implements IllegalService {

    @Override
    public Illegal add(Illegal illegal) {
        QueryWrapper<Illegal> wrapper=new QueryWrapper<>();
        wrapper.eq("url",illegal.getUrl());
        Illegal old=getOne(wrapper);
        if(old!=null)
        {
            throw  new BizException("非法链接已存在！");
        }
        illegal.setCreated(LocalDateTime.now());
        illegal.setUpdated(LocalDateTime.now());
        save(illegal);
        return illegal;
    }

    @Override
    public List<Illegal> getList() {
        return list();
    }

    @Override
    public Illegal delete(Long id) {
        Illegal old=getById(id);
        if(old==null)
        {
            throw new BizException("非法链接不存在！");
        }
        removeById(id);
        return old;
    }
}
